<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
        <title>AJ Utils-Methods 教程</title>
        <meta name="description" content="Small Java utilities with many useful functions.  Methods 类提供了动态检索和调用 Java 方法的实用工具" />
        <meta name="keywords" content="AJ Utils, ajaxjs, ajaxjs framework, java utilities, tools, helper, 反射,方法调用,Java" />
        <meta name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="preconnect" href="https://fonts.googleapis.com" />
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
        <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@200..900&family=Noto+Serif:ital,wght@0,100..900;1,100..900&display=swap&family=Noto+Sans+SC:wght@100..900&display=swap" />
        <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@100..900&family=Noto+Serif:ital,wght@0,100..900;1,100..900&display=swap" /> 
        <link rel="stylesheet" href="https://framework.ajaxjs.com/static/new-ui/css/common.css" />
        <link rel="stylesheet" href="https://iam.ajaxjs.com/asset/main.css"/>
        <link rel="icon" type="image/x-icon" href="https://framework.ajaxjs.com/aj-logo/logo.ico"/>
        <script src="https://framework.ajaxjs.com/static/aj-docs/common.js"></script>
        <script>
            var _hmt = _hmt || [];
            (function() {
              var hm = document.createElement("script");
              hm.src = "https://hm.baidu.com/hm.js?208c5aea11c52991bd1c3283e62ad0ce";
              var s = document.getElementsByTagName("script")[0];
              s.parentNode.insertBefore(hm, s);
            })();
        </script>
    </head>
    <body>
        <nav>
            <div>
                <div class="links">
                    <a href="/cn">🏠 首页</a>
                    | ⚙️ 源码:
                    <a target="_blank" href="https://github.com/lightweight-component/aj-util">Github</a>/<a target="_blank" href="https://gitcode.com/lightweight-component/aj-util">Gitcode</a>
                  
                    | 🚀<a href="">英语版本</a>
                </div>
                <h1>
                    <img src="https://framework.ajaxjs.com/aj-logo/logo.png" style="vertical-align: middle;height: 45px;margin-bottom: 6px;" />
                    AJ Utils
                </h1>
                <h3>小型、干净、简单的 Java 工具库
                </h3>
            </div>
        </nav>
        <div>
            <menu>
                
                <ul>
                    <li class="selected">
                        <a href="/cn">首页</a>
                    </li>
                </ul>
                <h3>Common 常用模块</h3>
                <ul>
                    <li><a href="/common/JsonUtil-cn/">JsonUtil</a></li>
                    <li><a href="/common/Base64Helper-cn/">Base64Helper</a></li>
                    <li><a href="/common/ConvertBasicValue-cn/">ConvertBasicValue</a></li>
                    <li><a href="/common/MessageDigestHelper-cn/">HashHelper</a></li>
                    <li><a href="/common/MapTool-cn/">MapTool</a></li>
                    <li><a href="/common/ObjectHelper-cn/">ObjectHelper</a></li>
                    <li><a href="/common/RandomTools-cn/">RandomTools</a></li>
                    <li><a href="/common/RegExpUtils-cn/">RegExpUtils</a></li>
                    <li><a href="/common/StrUtil-cn/">StrUtil</a></li>
                    <li><a href="/common/BoxLogger-cn/">BoxLogger</a></li>
                    <li><a href="/common/BytesHelper-cn/">BytesHelper</a></li>
                    <li><a href="/common/EncodeTools-cn/">UrlEncode</a></li>
                    <li><a href="/common/XmlHelper-cn/">XmlHelper</a></li>
                    <li><a href="/common/WebUtils-cn/">WebUtils</a></li>
                </ul>
                <h3>Date 日期处理</h3>
                <ul>
                    <li><a href="/date/intro-cn/">简介</a></li>
                    <li><a href="/date/convert-cn/">万能日期类型转换</a></li>
                    <li><a href="/date/formatter-cn/">日期格式化</a></li>
                </ul>
                <h3>Reflection 反射</h3>
                <ul>
                    <li><a href="/reflect/Methods-cn/">Methods</a></li>
                    <li><a href="/reflect/Types-cn/">Types</a></li>
                    <li><a href="/reflect/Clazz-cn/">Clazz</a></li>
                </ul>

                <h3>Input/Output 输入/输出</h3>
                <ul>
                    <li><a href="/io/FileHelper-cn/">FileHelper</a></li>
                    <li><a href="/io/Resources-cn/">Resources</a></li>
                    <li><a href="/io/StreamHelper-cn/">DataReader/DataWriter</a></li>
                    <li><a href="/io/ZipHelper-cn/">ZipHelper</a></li>
                </ul>

                <h3>HTTP 请求</h3>
                <ul>
                    <li><a href="/http_request/Get-cn">HTTP 请求</a></li>
                     <li><a href="/http_request/advanced-usage-cn/">高级用法</a></li>
                    <li><a href="/http_request/Base-cn/">Base</a></li>
                </ul>

                <h3>加密/解密</h3>
                <ul>
                    <li><a href="/cryptography/intro-cn/">简介</a></li>
                    <li><a href="/cryptography/flow-cn/">基本流程</a></li>
                    <li><a href="/cryptography/AesCrypto-cn/">AES/DES 加密解密</a></li>
                    <li><a href="/cryptography/RsaCrypto-cn/">RSA 加密解密</a></li>
                </ul>
            </menu>
            <article class="aj-text chinese">
                <h1>Methods</h1>
<p><code>Methods</code> 类提供了动态检索和调用方法以及处理方法参数的方法。这些方法有助于在运行时高效、方便地操作 Java 方法。</p>
<h2>方法</h2>
<h3>1. <code>getDeclaredMethod(Class&lt;?&gt; clz, String methodName)</code></h3>
<p>通过方法名检索类的声明方法。</p>
<ul>
<li><strong>参数说明：</strong>
<ul>
<li><code>clz</code>: 要检索方法的类。</li>
<li><code>methodName</code>: 方法的名称。</li>
</ul>
</li>
<li><strong>返回值:</strong> 检索到的方法，如果方法不存在则返回 <code>null</code>。</li>
</ul>
<p><strong>示例:</strong></p>
<pre><code class="language-java">Method method = Methods.getDeclaredMethod(String.class, &quot;length&quot;);
// method 将会是 String 类的 &quot;length&quot; 方法
</code></pre>
<h3>2. <code>getMethod(Object obj, String method, Class&lt;?&gt;... args)</code></h3>
<p>通过方法名和参数类型检索类或实例的方法。</p>
<ul>
<li><strong>参数说明：</strong>
<ul>
<li><code>obj</code>: 要检索方法的实例或类。</li>
<li><code>method</code>: 方法的名称。</li>
<li><code>args</code>: 方法的参数类型。</li>
</ul>
</li>
<li><strong>返回值:</strong> 检索到的方法，如果方法不存在则返回 <code>null</code>。</li>
</ul>
<p><strong>示例:</strong></p>
<pre><code class="language-java">Method method = Methods.getMethod(String.class, &quot;substring&quot;, int.class, int.class);
// method 将会是 String 类的 &quot;substring&quot; 方法，带有两个 int 参数
</code></pre>
<h3>3. <code>getMethod(Object obj, String method, Object... args)</code></h3>
<p>通过方法名和参数检索类或实例的方法。</p>
<ul>
<li><strong>参数说明：</strong>
<ul>
<li><code>obj</code>: 要检索方法的实例或类。</li>
<li><code>method</code>: 方法的名称。</li>
<li><code>args</code>: 方法的参数。</li>
</ul>
</li>
<li><strong>返回值:</strong> 检索到的方法，如果方法不存在则返回 <code>null</code>。</li>
</ul>
<p><strong>示例:</strong></p>
<pre><code class="language-java">Method method = Methods.getMethod(&quot;example&quot;, &quot;substring&quot;, 1, 3);
// method 将会是 String 类的 &quot;substring&quot; 方法，带有两个 int 参数
</code></pre>
<h3>4. <code>getMethodByUpCastingSearch(Class&lt;?&gt; clz, String method, Object arg)</code></h3>
<p>通过方法名和参数搜索方法，自动向上转型参数类型。</p>
<ul>
<li><strong>参数说明：</strong>
<ul>
<li><code>clz</code>: 要搜索方法的类。</li>
<li><code>method</code>: 方法的名称。</li>
<li><code>arg</code>: 方法的参数。</li>
</ul>
</li>
<li><strong>返回值:</strong> 检索到的方法，如果方法不存在则返回 <code>null</code>。</li>
</ul>
<p><strong>示例:</strong></p>
<pre><code class="language-java">Method method = Methods.getMethodByUpCastingSearch(List.class, &quot;add&quot;, &quot;example&quot;);
// method 将会是 List 类的 &quot;add&quot; 方法，带有一个 Object 参数
</code></pre>
<h3>5. <code>getDeclaredMethodByInterface(Class&lt;?&gt; clz, String method, Object arg)</code></h3>
<p>通过方法名和参数检索类的声明方法，自动向上转型参数类型。</p>
<ul>
<li><strong>参数说明：</strong>
<ul>
<li><code>clz</code>: 要检索方法的类。</li>
<li><code>method</code>: 方法的名称。</li>
<li><code>arg</code>: 方法的参数。</li>
</ul>
</li>
<li><strong>返回值:</strong> 检索到的方法，如果方法不存在则返回 <code>null</code>。</li>
</ul>
<p><strong>示例:</strong></p>
<pre><code class="language-java">Method method = Methods.getDeclaredMethodByInterface(List.class, &quot;add&quot;, &quot;example&quot;);
// method 将会是 List 类的 &quot;add&quot; 方法，带有一个 Object 参数
</code></pre>
<h2>单元测试示例</h2>
<p>以下是单元测试中的一些示例：</p>
<pre><code class="language-java">package com.ajaxjs.util.reflect;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class TestReflectUtil {
    public static class Foo {
        public Foo() {
        }

        public Foo(String str, String str2) {
        }

        public void Bar() {

        }

        public void CC(String cc) {

        }

        public String Bar2() {
            return &quot;bar2&quot;;
        }

        public String Bar3(String arg) {
            return arg;
        }
    }

    static class Foo2 {
        public void m1() {
        }

        public void m1(String arg) {
        }
    }

    static class Bar extends Foo {
        public void m2() {
        }
    }

    @Test
    public void testGetMethod() {
        assertNotNull(Methods.getMethod(new Foo(), &quot;m1&quot;));// 按实际对象
        assertNotNull(Methods.getMethod(Foo2.class, &quot;m1&quot;));// 按类引用
        assertNotNull(Methods.getMethod(Foo2.class, &quot;m1&quot;, String.class)); // 按参数类型
        assertNotNull(Methods.getMethod(Foo2.class, &quot;m1&quot;, &quot;foo&quot;));// 按实际参数
        assertNotNull(Methods.getMethod(Bar.class, &quot;m1&quot;));
        assertNotNull(Methods.getMethod(Bar.class, &quot;m1&quot;, String.class));
        assertNotNull(Methods.getMethod(Bar.class, &quot;m2&quot;));
    }

    static class Foo1 {
        public void foo(Foo1 a) {

        }
    }

    static class Bar2 extends Foo1 {

    }

    @Test
    public void testGetMethodByUpCastingSearch() {
        assertNull(Methods.getMethod(Foo1.class, &quot;foo&quot;, new Bar2())); // 找不到
        assertNotNull(Methods.getMethodByUpCastingSearch(Foo1.class, &quot;foo&quot;, new Bar2())); // 找到了
    }

    public static class A {
        public String foo(A a) {
            return &quot;A.foo&quot;;
        }

        public String bar(C c) {
            return &quot;A.bar&quot;;
        }
    }

    public static class B extends A {
    }

    public interface C {
    }

    public static class D implements C {
    }

    @Test
    public void testDeclaredMethod() {
        assertNull(Methods.getMethodByUpCastingSearch(A.class, &quot;bar&quot;, new D())); // 找不到
        assertNotNull(Methods.getDeclaredMethodByInterface(A.class, &quot;bar&quot;, new D()));// 找到了
    }

    public static class Foo3 {
        public void m1() {
        }

        public String m1(String arg) {
            return arg;
        }
    }

    static class Bar3 extends Foo3 {
        public void m2() {
        }
    }

    @Test
    public void testExecuteMethod() {
        assertNull(Methods.executeMethod(new Foo3(), &quot;m1&quot;));
        assertEquals(Methods.executeMethod(new Foo3(), &quot;m1&quot;, &quot;foo&quot;), &quot;foo&quot;);
        assertNull(Methods.executeMethod(new Bar2(), &quot;m1&quot;));
        assertEquals(Methods.executeMethod(new Bar3(), &quot;m1&quot;, &quot;bar&quot;), &quot;bar&quot;);
        assertEquals(Methods.executeMethod(new Bar3(), &quot;m1&quot;, String.class, &quot;foo&quot;), &quot;foo&quot;);
    }
}
</code></pre>

            </article>
        </div>

       <footer>
            AJ-Util，开源框架 <a href="https://framework.ajaxjs.com" target="_blank">AJ-Framework</a> 的一部分。联系方式：
            frank@ajaxjs.com，<a href="https://blog.csdn.net/zhangxin09" target="_blank">作者博客</a>
            <br />
            <br />
            Copyright © 2025 Frank Cheung. All rights reserved.
        </footer>
    </body>
</html>